package agent;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.TimerTask;

import mainBoerse.AccountManagerImpl;
import mainBoerse.Share;
import mainBoerse.UpdateTimer;
import stockPrice.StockPriceProvider;

public class PlayerAgent {

	private String playername;
	private String cashAcc;
	private AccountManagerImpl impl;

	public PlayerAgent(String player) throws IOException {
		this.playername = player.toUpperCase();
		this.cashAcc = playername + "cash";
		impl = AccountManagerImpl.getInstance();
	}

	private float getMoney() {
		// System.out.println(this.impl.countValueAsset(this.playername,
		// this.cashAcc));
		return this.impl.countValueAsset(this.playername, this.cashAcc);
	}

	public void process() throws Exception {
		while (impl.getSpecificPlayer(playername).getAgentStatus()) {
			ArrayList<Share> share = StockPriceProvider.getSHARES();

			/**
			 * buy cheapest sell the most expensive
			 * 
			 */
			if (getMoney() > 0) {

				buyCheapest(share);

			}
			// sell
			if (impl.getSpecificPlayer(playername)
					.searchShare(share.get(sellMostExpensiv(share)).getAktienName())
					.getSize() > 0) {
				int count = impl
						.getSpecificPlayer(playername)
						.searchShare(
								share.get(sellMostExpensiv(share)).getAktienName())
						.getSize();
				try {
					impl.sellShare(playername,
							share.get(sellMostExpensiv(share)).getAktienName(),
							count);
				} catch (Exception e) {
					throw e;
				}
			} /*else {
				for(Share Share :StockPriceProvider.getSHARES()) {
					int count = impl
							.getSpecificPlayer(playername)
							.searchShare(
									Share.getAktienName())
							.getSize();
					if(count > 0) impl.sellShare(this.playername, Share.getAktienName(), count);
				}
			}*/

			if (getMoney() >= 100000000) { // pause system
				Scanner pauser = new Scanner(System.in);

				System.out.println("PlayerCash Value more than one hundred Million");
				impl.getSpecificPlayer(playername).deactivateAgent();
				//pauser.nextLine();
			}
		}
	}

	private int cheapestShare(ArrayList<Share> shares) {
		int index = 1;
		for (int i = 0; i < shares.size(); i++) {
			if (shares.get(i).getAktienKurs() < shares.get(index).getAktienKurs()) {
				index = i;
			}
		}
		return index;
	}

	private void buyCheapest(ArrayList<Share> share) throws Exception {
		float currentMoney = getMoney();
		int cheapestShare = cheapestShare(share);
		int buyAmount = (int) (currentMoney / share.get(cheapestShare)
				.getAktienKurs());

		if (buyAmount >= 1 && (currentMoney >= (share.get(cheapestShare).getAktienKurs()*buyAmount)))
			this.impl.buyShare(playername,
					share.get(cheapestShare).getAktienName(), buyAmount);
		else return;
	}

	private int sellMostExpensiv(ArrayList<Share> share) {
		int index = 0;
		for (int i = 0; i < share.size(); i++) {
			if (share.get(i).getAktienKurs() > share.get(index).getAktienKurs())
				index = i;
			// else index= i+1;
		}
		return index;
	}

	public void processTimed() {
		UpdateTimer.startTimingWithOwnTimerTask(new TimerTask() {

			@Override
			public void run() {
				try {
					process();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}, 1000, 1000);
	}
}
